//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FactorSpreadedHazardRateCurve.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Credit/HazardRateStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures::Credit;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ originalCurve, Cephei::QL::IQuote^ spread) : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
    CDefaultProbabilityTermStructure^ _CoriginalCurve;
    CQuote^ _Cspread;
    try
    {
#ifdef HANDLE
        _phFactorSpreadedHazardRateCurve = NULL;
#endif
        _CoriginalCurve = safe_cast<CDefaultProbabilityTermStructure^> (originalCurve);
        _CoriginalCurve->Lock();
        Handle<QuantLib::DefaultProbabilityTermStructure>& _originalCurve = static_cast<Handle<QuantLib::DefaultProbabilityTermStructure>&> (_CoriginalCurve->GetHandle ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        _ppFactorSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve> (new QuantLib::FactorSpreadedHazardRateCurve ( _originalCurve,  _spread ));
        SetHazardRateStructure (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFactorSpreadedHazardRateCurve));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoriginalCurve != nullptr) _CoriginalCurve->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve>& childNative, Object^ owner) : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phFactorSpreadedHazardRateCurve = NULL;
#endif
	_ppFactorSpreadedHazardRateCurve = &childNative;
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFactorSpreadedHazardRateCurve));
}
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (QuantLib::FactorSpreadedHazardRateCurve& childNative, Object^ owner) : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phFactorSpreadedHazardRateCurve = NULL;
#endif
	_ppFactorSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve> (&childNative);
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFactorSpreadedHazardRateCurve));
    _FactorSpreadedHazardRateCurveOwner = owner;
    _HazardRateStructureOwner = owner;
}

Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (CFactorSpreadedHazardRateCurve^ copy) : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phFactorSpreadedHazardRateCurve = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFactorSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve> (copy->GetShared());
        _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFactorSpreadedHazardRateCurve));
    }
}
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (PLATFORM::Type^ t) : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phFactorSpreadedHazardRateCurve = NULL;
#endif
	if (!t->IsSubclassOf(CFactorSpreadedHazardRateCurve::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (QuantLib::Handle<QuantLib::FactorSpreadedHazardRateCurve>& childNative, Object^ owner)  : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
	_phFactorSpreadedHazardRateCurve = &childNative;
	_ppFactorSpreadedHazardRateCurve = &static_cast<boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve>>(childNative.currentLink());
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFactorSpreadedHazardRateCurve));
    _FactorSpreadedHazardRateCurveOwner = owner;
}
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (QuantLib::Handle<QuantLib::FactorSpreadedHazardRateCurve> childNative)  : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
	_phFactorSpreadedHazardRateCurve = &childNative;
	_ppFactorSpreadedHazardRateCurve = &static_cast<boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve>>(childNative.currentLink());
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFactorSpreadedHazardRateCurve));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::CFactorSpreadedHazardRateCurve (QuantLib::FactorSpreadedHazardRateCurve childNative)  : CHazardRateStructure(CFactorSpreadedHazardRateCurve::typeid)
{
#ifdef HANDLE
	_phFactorSpreadedHazardRateCurve = NULL;
#endif
	_ppFactorSpreadedHazardRateCurve = new boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve> (new QuantLib::FactorSpreadedHazardRateCurve (childNative));
    _ppHazardRateStructure = new boost::shared_ptr<QuantLib::HazardRateStructure> (boost::dynamic_pointer_cast<QuantLib::HazardRateStructure> (*_ppFactorSpreadedHazardRateCurve));
}
#endif

Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::~CFactorSpreadedHazardRateCurve ()
{
    if (_ppFactorSpreadedHazardRateCurve != NULL)
    {
	    delete _ppFactorSpreadedHazardRateCurve;
        _ppFactorSpreadedHazardRateCurve = NULL;
    }
}
Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::!CFactorSpreadedHazardRateCurve ()
{
    if (_ppFactorSpreadedHazardRateCurve != NULL)
    {
	    delete _ppFactorSpreadedHazardRateCurve;
    }
}
QuantLib::FactorSpreadedHazardRateCurve& Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::GetReference ()
{
    if (_ppFactorSpreadedHazardRateCurve == NULL) throw REFNEW NativeNullException ();
	return **_ppFactorSpreadedHazardRateCurve;
}
boost::shared_ptr<QuantLib::FactorSpreadedHazardRateCurve>& Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::GetShared ()
{
    if (_ppFactorSpreadedHazardRateCurve == NULL) throw REFNEW NativeNullException ();
	return *_ppFactorSpreadedHazardRateCurve;
}
QuantLib::FactorSpreadedHazardRateCurve* Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::GetPointer ()
{
    if (_ppFactorSpreadedHazardRateCurve == NULL) throw REFNEW NativeNullException ();
	return &**_ppFactorSpreadedHazardRateCurve;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FactorSpreadedHazardRateCurve>& Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::GetHandle ()
{
	if (_phFactorSpreadedHazardRateCurve == NULL)
	{
		_phFactorSpreadedHazardRateCurve = new Handle<QuantLib::FactorSpreadedHazardRateCurve> (*_ppFactorSpreadedHazardRateCurve);
	}
	return *_phFactorSpreadedHazardRateCurve;
}
#endif
bool Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::HasNative () 
{
	return (_ppFactorSpreadedHazardRateCurve != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppFactorSpreadedHazardRateCurve)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppFactorSpreadedHazardRateCurve)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppFactorSpreadedHazardRateCurve)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::MaxTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppFactorSpreadedHazardRateCurve)->maxTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppFactorSpreadedHazardRateCurve)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IFactorSpreadedHazardRateCurve^ Cephei::QL::Experimental::Credit::CFactorSpreadedHazardRateCurve_Factory::Create (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ originalCurve, Cephei::QL::IQuote^ spread)
{
    return REFNEW CFactorSpreadedHazardRateCurve ( originalCurve,  spread);
}
